use crate::cli::user::captcha::captcha;
use crate::cli::user::login::login;
use crate::cli::user::logout::logout;
use crate::cli::user::register::{register, UserRegister};
use crate::cli::user::show::show;
use clap::{command, Subcommand};
use regex::Regex;

/// 用户管理模块
#[derive(Subcommand, Debug, Clone)]
pub enum UserSubcommand {
    /// 查看用户信息
    Show {
        #[arg(short, long, default_value = "false")]
        json: Option<bool>,
    },
    ///注册用户
    #[command(long_about=None)]
    Register {
        /// 注册模式(必选)，
        /// 可选值有：1.对话模式：dialog，该模式下用户信息以交互式控制台方式传入；
        /// 2.直接模式：direct，该模式下用户信息直接通过指令传入。
        #[arg(short, long, value_parser=check_register_type)]
        model: String,
        ///用户名(可选)；在直接模式需指定该参数。
        #[arg(short, long)]
        account: Option<String>,
        ///昵称(可选)；
        #[arg(short, long)]
        nickname: Option<String>,
        ///密码(可选)；在直接模式需指定该参数。
        #[arg(short, long)]
        password: Option<String>,
        ///手机号(可选)；在直接模式下需指定该参数。
        #[arg(short='P', long, value_parser=check_phone)]
        phone: Option<String>,
        ///验证码(可选)；在直接模式需指定该参数。
        #[arg(short, long)]
        captcha: Option<String>,
    },
    /// 获取验证码
    Captcha {
        ///手机号(必选)
        #[arg(short, long, value_parser=check_phone)]
        phone: String,
    },
    /// 登出
    Logout {},
    /// 登录
    Login {
        ///登录模式(必选)，可选值有：1.验证码：phone；
        /// 2.账号密码：username。
        #[arg(short='t', long, value_parser=check_login_type)]
        login_type: String,
        ///用户名(必选)。
        ///账号密码登录时需指定该参数。
        #[arg(short, long)]
        account: Option<String>,
        /// 密码(可选)。
        ///账号密码登录时需指定该参数。
        #[arg(short = 'P', long)]
        password: Option<String>,
        ///手机号(可选)。
        /// 验证码登录时需指定该参数。
        #[arg(short, long, value_parser=check_phone)]
        phone: Option<String>,
        /// 验证码(可选)。
        /// 验证码登录时需指定该参数。
        #[arg(short, long)]
        captcha: Option<String>,
    },
}

fn check_login_type(login_type: &str) -> Result<String, String> {
    match login_type {
        "phone" => Ok("phone".to_string()),
        "username" => Ok("username".to_string()),
        _ => Err("不支持的登录类型".to_string()),
    }
}

fn check_register_type(register_type: &str) -> Result<String, String> {
    match register_type {
        "dialog" => Ok("dialog".to_string()),
        "direct" => Ok("direct".to_string()),
        _ => Err("不支持的注册模式".to_string()),
    }
}

fn check_phone(phone: &str) -> Result<String, String> {
    let regex = Regex::new(r"^1[3-9]\d{9}$").unwrap();
    if !regex.is_match(&phone) {
        return Err("电话号码不合法".to_string());
    }
    Ok(phone.to_string())
}

pub fn user_run(cmd: UserSubcommand) {
    match cmd {
        UserSubcommand::Register {
            model,
            account,
            nickname,
            password,
            phone,
            captcha,
        } => {
            let _ = register(UserRegister {
                model,
                account,
                password,
                nickname,
                phone,
                captcha,
            });
        }
        UserSubcommand::Login {
            login_type,
            account,
            password,
            phone,
            captcha,
        } => login(login_type, account, password, phone, captcha),
        UserSubcommand::Logout {} => logout(),
        UserSubcommand::Show { json } => show(json),
        UserSubcommand::Captcha { phone } => captcha(phone),
    }
}
